
Public Class Organization

    Protected _organization_id As String
    Protected _organization_name_tha As String
    Protected _organization_name_eng As String
    Protected _organization_parent_id As String
    Protected _use_status As Boolean
    Protected _province As New Province()
    Protected _Data As ArrayList
    Protected _DataCurrent As Integer

    Public Property organization_id() As String
        Get
            Return _organization_id
        End Get
        Set(ByVal value As String)
            _organization_id = value
        End Set
    End Property

    Public Property organization_name_tha() As String
        Get
            Return _organization_name_tha
        End Get
        Set(ByVal value As String)
            _organization_name_tha = value
        End Set
    End Property

    Public Property organization_name_eng() As String
        Get
            Return _organization_name_eng
        End Get
        Set(ByVal value As String)
            _organization_name_eng = value
        End Set
    End Property

    Public Property organization_parent_id() As String
        Get
            Return _organization_parent_id
        End Get
        Set(ByVal value As String)
            _organization_parent_id = value
        End Set
    End Property

    Public Property use_status() As Boolean
        Get
            Return _use_status
        End Get
        Set(ByVal value As Boolean)
            _use_status = value
        End Set
    End Property

    Public Property province() As Province
        Get
            Return _province
        End Get
        Set(ByVal value As Province)
            _province = value
        End Set
    End Property

    Public Function getOrganizationAll() As System.Data.DataSet
        Dim outResult As New System.Data.DataSet()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationAll"

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        objDa.Fill(outResult, "tableOrganization")

        Return outResult
    End Function

    Public Function getAllOrganization() As ArrayList
        Dim outResult As New ArrayList()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationAll"

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableOrganization")

        For Each objDr As System.Data.DataRow In objDs.Tables("tableOrganization").Rows
            Dim obj As Organization = New Organization()
            obj.organization_id = objDr.Item("organization_id").ToString()
            obj.organization_name_tha = objDr.Item("organization_name_tha").ToString()
            obj.organization_name_eng = objDr.Item("organization_name_eng").ToString()
            obj.organization_parent_id = objDr.Item("organization_parent_id").ToString()
            obj.province.province_id = objDr.Item("province_id").ToString()
            obj.province.province_name_eng = objDr.Item("province_name_eng").ToString()
            obj.province.province_name_tha = objDr.Item("province_name_tha").ToString()
            obj.use_status = objDr.Item("use_status").ToString()
            outResult.Add(obj)
        Next

        Return outResult
    End Function

    Public Function getOrganizationRoot() As ArrayList
        Dim outResult As New ArrayList()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationRoot"

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableOrganization")

        For Each objDr As System.Data.DataRow In objDs.Tables("tableOrganization").Rows
            Dim obj As Organization = New Organization()
            obj.organization_id = objDr.Item("organization_id").ToString()
            obj.organization_name_tha = objDr.Item("organization_name_tha").ToString()
            obj.organization_name_eng = objDr.Item("organization_name_eng").ToString()
            obj.organization_parent_id = objDr.Item("organization_parent_id").ToString()
            obj.province.province_id = objDr.Item("province_id").ToString()
            obj.province.province_name_eng = objDr.Item("province_name_eng").ToString()
            obj.province.province_name_tha = objDr.Item("province_name_tha").ToString()
            obj.use_status = objDr.Item("use_status").ToString()
            outResult.Add(obj)
        Next

        Return outResult
    End Function

    Public Sub getOrganizationParent(ByVal inOrganizationID As String)
        Dim outResult As New System.Data.DataSet()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationParent"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", inOrganizationID)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        objDa.Fill(outResult, "tableOrganization")

        If outResult.Tables("tableOrganization").Rows.Count <> 0 Then
            Me.organization_id = outResult.Tables("tableOrganization").Rows(0).Item("organization_id").ToString()
            Me.organization_name_tha = outResult.Tables("tableOrganization").Rows(0).Item("organization_name_eng").ToString()
            Me.organization_name_eng = outResult.Tables("tableOrganization").Rows(0).Item("organization_name_eng").ToString()
            Me.organization_parent_id = outResult.Tables("tableOrganization").Rows(0).Item("organization_parent_id").ToString()
            Me.province.province_id = outResult.Tables("tableOrganization").Rows(0).Item("province_id").ToString()
            Me.province.province_name_eng = outResult.Tables("tableOrganization").Rows(0).Item("province_name_eng").ToString()
            Me.province.province_name_tha = outResult.Tables("tableOrganization").Rows(0).Item("province_name_tha").ToString()
            Me.use_status = outResult.Tables("tableOrganization").Rows(0).Item("use_status").ToString()
        End If
    End Sub

    Public Function getOrganizationChild(ByVal inOrganizationID As String) As System.Data.DataSet
        Dim outResult As New System.Data.DataSet()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationChild"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationParentID", inOrganizationID)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        objDa.Fill(outResult, "tableOrganization")

        Return outResult
    End Function

    Public Function getOrganizationChilds(ByVal inOrganizationID As String) As ArrayList
        Dim outResult As New ArrayList()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationChild"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationParentID", inOrganizationID)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableOrganization")

        For Each objDr As System.Data.DataRow In objDs.Tables("tableOrganization").Rows
            Dim obj As Organization = New Organization()
            obj.organization_id = objDr.Item("organization_id").ToString()
            obj.organization_name_tha = objDr.Item("organization_name_tha").ToString()
            obj.organization_name_eng = objDr.Item("organization_name_eng").ToString()
            obj.organization_parent_id = objDr.Item("organization_parent_id").ToString()
            obj.province.province_id = objDr.Item("province_id").ToString()
            obj.province.province_name_eng = objDr.Item("province_name_eng").ToString()
            obj.province.province_name_tha = objDr.Item("province_name_tha").ToString()
            obj.use_status = objDr.Item("use_status").ToString()
            outResult.Add(obj)
        Next

        Return outResult
    End Function

    Public Function getOrganizationParentAndChild(ByVal inOrganizationID As String) As System.Data.DataSet
        Dim outResult As New System.Data.DataSet()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationParentAndChild"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", inOrganizationID)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        objDa.Fill(outResult, "tableOrganization")

        Return outResult
    End Function

    Protected Function getOrganizationParentAndChilds(ByVal inOrganization As String) As ArrayList
        Dim outResult As New ArrayList()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationParentAndChild"

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableOrganization")

        For Each objDr As System.Data.DataRow In objDs.Tables("tableOrganization").Rows
            Dim obj As Organization = New Organization()
            obj.organization_id = objDr.Item("organization_id").ToString()
            obj.organization_name_tha = objDr.Item("organization_name_eng").ToString()
            obj.organization_name_eng = objDr.Item("organization_name_eng").ToString()
            obj.organization_parent_id = objDr.Item("organization_parent_id").ToString()
            obj.province.province_id = objDr.Item("province_id").ToString()
            obj.use_status = objDr.Item("use_status").ToString()
            outResult.Add(obj)
        Next

        Return outResult
    End Function

    Public Function getOrganizationID(ByVal inOrganizationNameTha As String) As String
        Dim outResult As String = Nothing
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_OrganizationAll"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationNameTha", inOrganizationNameTha)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableOrganization")

        If objDs.Tables(0).Rows.Count <> 0 Then
            For Each objDR As Data.DataRow In objDs.Tables(0).Rows
                If inOrganizationNameTha = objDR.Item("organization_name_tha").ToString() Then
                    outResult = objDR.Item("organization_id").ToString()
                End If
            Next
        End If

        Return outResult
    End Function

    Public Sub LoadAllOrganizationAll()
        _Data = New ArrayList
        _DataCurrent = 0
        _Data = getAllOrganization()
    End Sub

    Public Sub LoadOrganizationChild(ByVal inOrganization As String)
        _Data = New ArrayList
        _DataCurrent = 0
        _Data = getOrganizationChilds(inOrganization)
    End Sub

    Public Sub LoadOrganizationParentAndChild(ByVal inOrganization As String)
        _Data = New ArrayList
        _DataCurrent = 0
        _Data = getOrganizationParentAndChilds(inOrganization)
    End Sub

    Public Function NextRecord() As Boolean
        If ((Not _Data Is Nothing) AndAlso (((Not _Data Is Nothing) AndAlso _
            (_DataCurrent < _Data.Count And _Data.Count > 0)))) Then
            _organization_id = _Data(_DataCurrent).organization_id
            _organization_name_tha = _Data(_DataCurrent).organization_name_tha
            _organization_name_eng = _Data(_DataCurrent).organization_name_eng
            _organization_parent_id = _Data(_DataCurrent).organization_parent_id
            _province.province_id = _Data(_DataCurrent).province.province_id
            _province.province_name_eng = _Data(_DataCurrent).province.province_name_eng
            _province.province_name_tha = _Data(_DataCurrent).province.province_name_tha
            _use_status = _Data(_DataCurrent).use_status
            _DataCurrent += 1
            Return True
        End If
        Return False
    End Function

    Public Shared Function GetOrganizationByOrganizationId(ByVal inOrganizationId As String) As Organization
        Dim outResult As New Organization()
        Dim decryptText As New DateUtilities.Encrypt_Data()
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
            decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
            "PoliceHealthConnectionString").ToString(), True))
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
        objCmd.Connection = objConn
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.CommandText = "proc_SELECT_Organization"

        Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", inOrganizationId)
        objCmd.Parameters.Add(objParam)

        Dim objDa As New MySql.Data.MySqlClient.MySqlDataAdapter(objCmd)
        Dim objDs As New System.Data.DataSet()
        objDa.Fill(objDs, "tableOrganization")

        If objDs.Tables(0).Rows.Count <> 0 Then
            For Each objDR As Data.DataRow In objDs.Tables(0).Rows
                outResult.organization_id = objDR.Item("organization_id").ToString()
                outResult.organization_name_eng = objDR.Item("organization_name_eng").ToString()
                outResult.organization_name_tha = objDR.Item("organization_name_tha").ToString()
                outResult.organization_parent_id = objDR.Item("organization_parent_id").ToString()
                outResult.use_status = CBool(objDR.Item("use_status").ToString())
            Next
        End If

        Return outResult
    End Function

End Class
